<template>
  <div>
    <div class="query_form">
      <el-form ref="queryData" :model="queryData">
        <el-form-item>
          <el-input suffix-icon="el-icon-search" placeholder="请输入昵称" v-model="queryData.nickname"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input suffix-icon="el-icon-s-cooperation" placeholder="请输入主修" v-model="queryData.major"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input suffix-icon="el-icon-message-solid" placeholder="请输入喜爱动漫" v-model="queryData.loveComic"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="query">查询</el-button>
          <el-button type="warning" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-button type="primary" @click="add" icon="el-icon-circle-plus-outline">新增</el-button>
      <el-button type="danger" @click="deleteBatch" icon="el-icon-delete">批量删除</el-button>
      <el-button type="success" @click="importExcel" icon="el-icon-download">导入</el-button>
      <el-button type="success" @click="exportExcel" icon="el-icon-upload2">导出</el-button>
    </div>
    <el-table
        :data="tableData"
        stripe
        style="width: 100%">
      <el-table-column
          prop="username"
          label="用户名"
          width="180">
      </el-table-column>
      <el-table-column
          prop="password"
          label="密码"
          width="180">
      </el-table-column>
      <el-table-column
          prop="nickname"
          label="昵称">
      </el-table-column>
      <el-table-column
          label="状态">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status === 1">良好</el-tag>
          <el-tag type="danger" v-if="scope.row.status === 0">不良</el-tag>
        </template>
      </el-table-column>
      <el-table-column
          prop="slogan"
          label="标语">
      </el-table-column>
      <el-table-column
          prop="major"
          label="主修">
      </el-table-column>
      <el-table-column
          prop="loveComic"
          label="喜爱动漫">
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              style="margin-right: 5px"
              size="mini"
              icon="el-icon-edit"
              type="primary"
              @click="handleEdit(scope.row)">编辑</el-button>

          <el-popconfirm
              title="是否确认删除?"
              @confirm="handleDelete(scope.row.id)"
              @cancel="cancel"
          >
            <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                slot="reference">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!--分页插件-->
    <div class="page">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="paginationData.currentPage"
          :page-sizes="[5, 10, 20, 50, 100]"
          :page-size="paginationData.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paginationData.total">
      </el-pagination>
    </div>

    <!--编辑 & 新增弹层-->
    <el-dialog
        @close="closeDialogAndClearFormData"
        width="500px"
        :title="isAddDialog ? '新增' : '编辑'"
        :visible.sync="dialogVisible"
        center>
        <el-form class="dialog_form" :model="formData" :rules="rules" ref="formData" label-width="100px">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="formData.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="formData.password"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="formData.nickname"></el-input>
          </el-form-item>
          <el-form-item label="可用" prop="status">
            <el-switch active-color="#13ce66"
                       inactive-color="#ff4949"
                       v-model="formData.status"
                       :active-value="1"
                       :inactive-value="0">
            </el-switch> <!--boolean型变量-->
          </el-form-item>
          <el-form-item label="标语" prop="slogan">
            <el-input style="width: 300px" type="textarea" v-model="formData.slogan"></el-input>
          </el-form-item>
          <el-form-item label="主修" prop="major">
            <el-select style="width: 300px" v-model="formData.major" placeholder="请选择主修">
              <el-option v-for="item in major" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="喜爱动漫" prop="loveComic">
            <el-select style="width: 300px" v-model="formData.loveComic" placeholder="请选择喜爱动漫">
              <el-option v-for="item in loveComic" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-form-item>

          <el-form-item class="form_button">
            <el-button type="primary" @click="submitForm('formData')">{{isAddDialog ? "添加" : "保存"}}</el-button>
            <el-button @click="cancelDialog">取消</el-button>
          </el-form-item>

        </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Index",
  data() {
    return {
      major: [
        "计算机科学与技术",
        "人工智能",
        "通信工程",
        "网络安全",
        "中医",
        "航空航天",
        "土木工程",
        "新东方",
        "总统学",
        "富婆学"
      ],
      loveComic: [
        "电锯人",
        "赛博朋克：边缘行者",
        "死亡笔记",
        "进击的巨人",
        "魔法少女小圆",
        "奇巧计程车",
        "双城之战",
        "RNG vs SKT",
        "命运石之门",
        "托利托塔娜"
      ],
      isAddDialog: false,
      formData: {
        nickname: '',
        major: '',
        loveComic: '',
        id: '',
        username: '',
        password: '',
        status: 1,
        slogan: '',
        //为status多加一个字段
        otherStatus: true
      },
      //查询表单信息单独分开
      queryData: {
        nickname: '',
        major: '',
        loveComic: ''
      },
      dialogVisible: false,
      tableData: [], // 包含id
      paginationData: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' },
        ],
        slogan: [
          { required: true, message: '请输入标语', trigger: 'blur' },
        ],
        major: [
          { required: true, message: '请选择主修', trigger: 'change' }
        ],
        loveComic: [
          { required: true, message: '请选择喜爱动漫', trigger: 'change' }
        ],
      }
    }
  },
  created() {
    this.query()
  },
  methods: {
    closeDialogAndClearFormData() {
      this.$refs.formData.resetFields()
    },
    cancelDialog() {
      this.dialogVisible = false
      this.$message.info('取消成功')
    },
    closeDialog() {
      this.dialogVisible = false
      this.$refs.formData.resetFields()
    },
    resetForm() {
      this.$refs.formData.resetFields()
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        // this.formData.status = this.formData.status
        if (valid) {
          //根据otherStatus转化为status
          // this.formData.status = this.formData.otherStatus ? 1 : 0
          if (this.isAddDialog) {
            //若为新增操作
            this.$axios.post('/common-user/add', this.formData).then(res => {
              this.closeDialog()
              this.$message.success('添加成功!')
              //清空表单
              this.resetForm()
              this.query()
            })
          } else {
            //若为编辑操作
            //提示
            this.$confirm('是否确认保存', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              this.$axios.put('/common-user/edit', this.formData).then(res => {
                this.closeDialog()
                this.$message.success('保存成功!')
                this.resetForm()
                this.query()
              })
            }).catch(() => {
              this.$message({
                type: 'info',
                message: '取消成功'
              });
            });
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleSizeChange(pageSize) {
      this.paginationData.pageSize = pageSize
      this.query()
    },
    handleCurrentChange(currentPage) {
      this.paginationData.currentPage = currentPage
      this.query()
    },
    cancel() {
      this.$message("取消成功")
    },
    handleEdit(row) {
      //回显表单
      this.$axios.get('/common-user/getOne/' + row.id).then(res => {
        this.formData = res.data.data
        //表示为编辑弹层
        this.isAddDialog = false
        this.dialogVisible = true
      })
    },
    handleDelete(id) {
      this.$axios.delete('/common-user/delete/' + id).then(res => {
        this.$message.success('删除成功!')
        this.query()
      })
    },
    query() {
      //拼接查询信息
      let params = "?query=1&nickname=" + this.queryData.nickname + "&major=" + this.queryData.major + "&loveComic=" + this.queryData.loveComic
      //获取分页数据
      this.$axios.get("/common-user/get/" + this.paginationData.currentPage + "/" + this.paginationData.pageSize + params).then(res => {
        this.tableData = res.data.data.records
        this.paginationData.currentPage = res.data.data.current
        this.paginationData.pageSize = res.data.data.size
        this.paginationData.total = res.data.data.total
      })
    },
    reset() {
      Object.keys(this.queryData).forEach(key => (this.queryData[key] = ''))
    },
    add() {
      this.dialogVisible = true
      //清空表单
      this.$refs.formData.resetFields()
      //otherStatus默认为true
      this.formData.otherStatus = true
      //表示是新增弹层
      this.isAddDialog = true
    },
    deleteBatch() {

    },
    importExcel() {

    },
    exportExcel() {

    }
  }
}
</script>

<style scoped>
.form_button {
  margin-left: 60px;
}

.dialog_form .el-input {
  width: 300px;
}

.page {
  text-align: center;
}

.query_form .el-form {
  display: flex;
  height: 50px;

}

.query_form .el-form-item {
  width: 300px;
  margin-right: 5px;
}
</style>